from functools import reduce
from collections import Counter

with open('input.txt') as file:
    could_be_in = {}
    
    ingredients_counter = Counter()

    for line in file.read().splitlines():
        # pmctk hnjj drtg (contains nuts, fish)
        ingredients, allergens = line.split(" (contains ")
        ingredients = set(ingredients.split())
        allergens = allergens[:-1].split(", ")

        ingredients_counter.update(ingredients)

        for allergen in allergens:
            if allergen in could_be_in:
                could_be_in[allergen] &= ingredients
            else:
                could_be_in[allergen] = set(ingredients)

    could_be_allergic = reduce(set.__or__, could_be_in.values(), set())
    print(sum(c for i, c in ingredients_counter.items() if i not in could_be_allergic))